package com.example.administrator.demoa;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements InOnTimeChangeListener{
    private Button btnStart;
    private Button btnStop;
    private Button btnBind;
    private Button btnUnBind;
    private ServiceConnection conn;
    private MyBinder binder;
    private Button btnData;
    private TextView txtWord;
    private EditText edtWord;
    private TextView txtTime;
    private MyReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("MainActivity", "thread id = "+Thread.currentThread().getId());

        receiver = new MyReceiver(MainActivity.this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("TIME_CHANGE");
        registerReceiver(receiver, filter);

        findViews();

        initConn();

        onBtnStart();

        onBtnStop();

        onBtnBind();

        onBtnUnBind();

        onBtnData();
    }

    private void initConn() {
        //ServiceConnection对象的创建
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder iBinder) {
                Log.e("MainActivity", "onServiceConnected");
                binder = (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.e("MainActivity", "onServiceDisconnected");
            }
        };
    }

    private void onBtnData() {
        btnData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = binder.reverseEveryWord(edtWord.getText().toString());
                String time = binder.getTimeChange();
                txtWord.setText(str + "\n" + time);
            }
        });
    }

    private void onBtnUnBind() {
        //解除绑定
        btnUnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(conn);
            }
        });
    }

    private void onBtnBind() {
        //绑定服务
        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                bindService(intent, conn, BIND_AUTO_CREATE);
            }
        });
    }

    private void onBtnStop() {
        //停止服务
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                stopService(intent);
            }
        });
    }

    private void onBtnStart() {
        //启动服务
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                startService(intent);
            }
        });
    }

    private void findViews() {
        btnStart = findViewById(R.id.btn_start);
        btnStop = findViewById(R.id.btn_stop);
        btnBind = findViewById(R.id.btn_bind);
        btnUnBind = findViewById(R.id.btn_unbind);
        txtWord = findViewById(R.id.txt_word);
        edtWord = findViewById(R.id.edt_word);
        btnData = findViewById(R.id.btn_data);
        txtTime = findViewById(R.id.txt_time);
    }

    @Override
    public void show(String str) {
        txtTime.setText(str);
    }

    /*//定义内部类
    private class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String str = intent.getStringExtra("TIME");
            txtTime.setText(str);
        }
    }*/
}
